<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
   <meta name="GENERATOR" content="Mozilla/4.7 [en] (WinNT; U) [Netscape]">
   <title>Fluents: a Uniform Extension of Kernel Prolog for Reflection and Interoperation with External Objects</title>
</head>
<body>

<center>
<h1>
<font face=""><font size=+3>Fluents: a Uniform Extension of Kernel Prolog
for Reflection and Interoperation with External Objects</font></font></h1></center>

<center>
<h3>
<b><font face=""><font size=+2>Paul Tarau<br>
</font></font>University of North Texas &amp; BinNet Corporation</b></h3></center>

<center>
<h3>
<font face=""><font size=+2><a href="http://www.cs.unt.edu/~tarau">http://www.cs.unt.edu/~tarau<br>
</a><i><a href="mailto:tarau@cs.unt.edu">tarau@cs.unt.edu</a></i></font></font></h3></center>

<ul>
<li>
<font face=""><font size=+3>Kernel Prolog = Horn Clause Interpreters with
LD-resolution ( <b>Answer Sources</b>)+ <b>Fluents</b></font></font></li>

<li>
<font face=""><font size=+3>redesigning key components of Prolog's system
of <i>built-in predicates</i></font></font></li>

<li>
<font face=""><font size=+3>uniform interface for controlling multiple
interpreters and external stateful objects</font></font></li>

<li>
<font face=""><font size=+3>lazy, composable data structures</font></font></li>

<li>
<font face=""><font size=+3>interoperation with the underlying Java system</font></font></li>

<li>
<font face=""><font size=+3>crisp abstractions -> new design patterns</font></font></li>

<li>
<b><font face=""><font size=+3><a href="http://www.binnetcorp.com/kprolog/Main.html">http://www.binnetcorp.com/kprolog/Main.html</a></font></font></b></li>
</ul>

<h1>
&nbsp;
<hr WIDTH="100%"></h1>

<h1>
Motivation</h1>

<ul>
<li>
<font face=""><font size=+3>Logic Programming languages share a common
kernel: <i>Horn Clause Resolution</i></font></font></li>

<li>
<i><font size=+3>LP: reasoning with referentially transparent, stateless
entities</font></i></li>

<li>
<i><font face=""><font size=+3>the resolution process as such, is <b>not</b>
stateless, as it proceeds in time, step by step</font></font></i></li>

<li>
<font face=""><font size=+3>ability to <i>reflect</i> in the object language
the resolution process provided by the underlying <i>''glass-box''</i>
interpreter, in its full generality => the need of <i>stateful</i> primitives</font></font></li>

<li>
<font face=""><font size=+3>similar abstractions:</font></font></li>

<ul>
<li>
<font face=""><font size=+3>file or socket streams in C, lazy list streams
in Scheme</font></font></li>

<li>
<font face=""><font size=+3>iterators in Java or C+, declarative I/O in
Mercury</font></font></li>

<li>
<font face=""><font size=+3>monadic constructs in Haskell and LambdaProlog</font></font></li>
</ul>

<hr WIDTH="100%">
<ul>
<h1>
Fluent Classes and their Operations</h1>
</ul>
</ul>
<font face=""><font size=+2>Fluents are created with specific <i>constructors</i>,
usually by converting from other Fluents or conventional Prolog data structures
like Terms, Lists or Databases...</font></font>
<pre><font face=""><font size=+2>class Fluent extends SystemObject {
&nbsp; Fluent(Prog p) {
&nbsp;&nbsp;&nbsp; trailMe(p);
&nbsp; }

&nbsp; // add the fluent to the parent Interpreter's Trail
&nbsp; protected void trailMe(Prog p) {
&nbsp;&nbsp;&nbsp; if(null!=p) p.getTrail().push(this);
&nbsp; }

&nbsp; // usable (through overriding) to release resources
&nbsp; // and/or stop ongoing computations

&nbsp; public void stop() {
&nbsp; }

&nbsp; // release resources on backtracking, if needed

&nbsp; protected void undo() {
&nbsp;&nbsp;&nbsp; stop();
&nbsp; }
}</font></font></pre>

<pre>

<hr WIDTH="100%"></pre>
<b><font face="Courier New"><font size=+3>Sources:</font></font></b>
<p><font face="Courier New"><font size=+2>abstract class Source extends
Fluent {</font></font>
<br><font face="Courier New"><font size=+2>&nbsp; Source(Prog p) {</font></font>
<br><font face="Courier New"><font size=+2>&nbsp;&nbsp;&nbsp; super(p);</font></font>
<br><font face="Courier New"><font size=+2>&nbsp; }</font></font>
<br><font face="Courier New"><font size=+2>&nbsp; abstract public Term
get();</font></font>
<br><font face="Courier New"><font size=+2>}</font></font>
<br>
<hr WIDTH="100%">
<br><b><font face=""><font size=+3>Sinks</font></font></b>
<p><font face="Courier New"><font size=+2>abstract class Sink extends Fluent
{</font></font>
<br><font face="Courier New"><font size=+2>&nbsp; Sink(Prog p) {</font></font>
<br><font face="Courier New"><font size=+2>&nbsp;&nbsp; super(p);</font></font>
<br><font face="Courier New"><font size=+2>&nbsp; }</font></font>
<br><font face="Courier New"><font size=+2>&nbsp; // sends T to the Sink
for tasks as</font></font>
<br><font face="Courier New"><font size=+2>&nbsp; // accumulation or printing</font></font>
<br><font face="Courier New"><font size=+2>&nbsp; abstract public int put(Term
T);</font></font>
<p><font face="Courier New"><font size=+2>&nbsp; // return data previously
sent to the Sink</font></font>
<br><font face="Courier New"><font size=+2>&nbsp; // (if collection ability
is present)</font></font>
<br><font face="Courier New"><font size=+2>&nbsp; public Term collect()
{&nbsp;&nbsp;&nbsp;&nbsp; return null;</font></font>
<br><font face="Courier New"><font size=+2>&nbsp; }</font></font>
<br><font face="Courier New"><font size=+2>}</font></font>
<br>
<hr WIDTH="100%">.
<h1>
Fluent Composers</h1>
<i><font face=""><font size=+2>Fluent composers</font></font> <font size=+2>provide
abstract operations on <b>Fluents</b>. They are usually implemented with
lazy semantics.</font></i>
<ul>
<li>
<b><font face=""><font size=+2>append_sources/3</font></font> <font size=+2>creates
a new Source with a get/2 operation such that when the first Source is
stopped, iteration continues over the elements of the second Source</font></b></li>

<li>
<b><font face=""><font size=+2>Compose_sources/3</font></font> <font size=+2>provides
a cartesian product style composition, the new get/2 operation returning
pairs of elements of the first and second Source</font></b></li>

<li>
<b><font face=""><font size=+2>Split_source/3</font></font> <font size=+2>creates
two Source objects identical to the Source given as first argument. It
allows writing programs which iterate over a given Source multiple times.</font></b></li>

<li>
<b><font face=""><font size=+2>Sources</font></font> <font size=+2>and
Sinks are related through a discharge(Source,Sink) operation which sends
all the elements of the Source to the given Sink.</font></b></li>
</ul>
<font face=""><font size=+2>Fluent Modifiers: ex. <b>set_persistent(Fluent,YesNo)</b>
is used to make a Fluent survive failure, by disabling its <b>undo</b>
method, which, by default, applies the Fluent's <b>stop</b> method on backtracking.</font></font>
<h1>

<hr WIDTH="100%"></h1>

<h1>
Interpreters as Answer Sources</h1>
<b><i><font face=""><font size=+2>Answer Sources</font></font></i> <font size=+2>can
be seen as generalized <i>iterators</i>, allowing a given program to control
<i>answer
production</i> in another. Each Answer Source works as a separate Horn
Clause LD-resolution <i>interpreter</i>.</font></b>
<p><font face=""><font size=+2>The <b>Answer Source</b> constructor initializes
a new interpreter.</font></font>
<p><font face="Courier New"><font size=+2>answer_source(AnswerPattern,Goal,AnswerSource)</font></font>
<ul>
<li>
<font face=""><font size=+2>creates a new Horn Clause solver, uniquely
identified by <b>AnswerSource</b> which shares code with the currently
running program and is initialized with resolvent <b>Goal</b>.</font></font></li>
</ul>
<font face=""><font size=+2><b>get/2</b>&nbsp; is used to retrieve successive
answers generated by an Answer Source, on demand</font></font>
<p><font face="Courier New"><font size=+2>get(AnswerSource,AnswerInstance)</font></font>
<ul>
<li>
<font face=""><font size=+2>tries to harvest the answer computed starting
from <b>Goal</b>, as a instance of <b>AnswerPattern</b></font></font></li>

<li>
<font face=""><font size=+2>if an answer is found it is returned as <b>the(AnswerInstance)</b>,
otherwise <b>no</b> is returned.</font></font></li>
</ul>
<font face=""><font size=+2>The <b>stop/1</b> operation is called automatically
when no more answers can be produced as well as through the Fluent's <b>undo</b>
operation on backtracking.</font></font>
<br>
<hr WIDTH="100%">
<h1>
<tt><b>first_solution/3, once/1</b> and not/1</tt></h1>

<pre><font face=""><font size=+2>% returns the(X) or no as first solution of G

first_solution(X,G,Answer):-&nbsp;
&nbsp; answer_source(X,G,Solver),
&nbsp; get(Solver,R),
&nbsp; stop(Solver),
&nbsp; eq(Answer,R).

% succeeds by binding G to its first solution or fails

once(G):-first_solution(G,G,the(G)).

% succeeds without binding G, if G fails

not(G):-first_solution(_,G,no).</font></font></pre>

<pre>
<hr WIDTH="100%"></pre>

<h1>
Reflective Meta-Interpreters</h1>

<pre><font face=""><font size=+2>metacall(Goal):-
&nbsp; answer_source(Goal,Goal,E),
&nbsp; element_of(E,Goal).

element_of(I,X):-get(I,the(A)),select_from(I,A,X).

select_from(_,A,A).
select_from(I,_,X):-element_of(I,X).</font></font></pre>

<pre>
<hr WIDTH="100%"></pre>

<h1>
Unfolders: fine grained control</h1>

<pre><font face=""><font size=+2>unfolder_source(Clause,Source)</font></font></pre>
<font size=+2>associative clause composition operation <font face="symbol">&Aring;</font>
:</font>
<p><tt><font size=+2>(A<sub>0</sub>:-A<sub>1</sub>,A<sub>2</sub>,...,A<sub>n</sub>)</font></tt><font size=+2><font face="symbol">&Aring;</font><tt>(B<sub>0</sub>:-B<sub>1</sub>,...,B<sub>m</sub>)
= (A<sub>0</sub>:-B<sub>1</sub>,...,B<sub>m</sub>,A<sub>2</sub>,...,A<sub>n</sub>)</tt><font face="symbol">q</font></font>
<p><font size=+2>with <font face="symbol">q</font> = mgu(<tt>A<sub>1</sub></tt>,<tt>B<sub>0</sub></tt>).</font>
<p><tt><font size=+2><font face="symbol">^</font> <font face="symbol">&Aring;</font>
C = C <font face="symbol">&Aring;</font> <font face="symbol">^</font> =
<font face="symbol">^</font></font></tt>
<pre><font face=""><font size=+2>unfold_solve(Goal):-
&nbsp; unfold(':-'(Goal,Goal),':-'(Goal,true)).

unfold(Clause,Clause).
unfold(Clause,Answer):-
&nbsp; unfolder_source(Clause,Unfolder),
&nbsp; element_of(Unfolder,NewClause),

&nbsp; unfold(NewClause,Answer).</font></font></pre>

<pre>
<hr WIDTH="100%"></pre>

<h1>
If-then-else</h1>

<pre><font face=""><font size=+2>% if Cond succeeds executes Then otherwise Else

if(Cond,Then,Else):-
&nbsp; first_solution(successful(Cond,Then),Cond,R),
&nbsp; select_then_else(R,Cond,Then,Else).

select_then_else(the(successful(Cond,Then)),Cond,Then,_):-Then.
select_then_else(no,_,_,Else):-Else.</font></font></pre>

<pre>
<hr WIDTH="100%"></pre>

<h1>
Findall/3: by recursing over get/2</h1>

<pre><font face=""><font size=+2>findall(X,G,Xs):-
&nbsp;&nbsp; answer_source(X,G,E),
&nbsp;&nbsp; get(E,Answer),
&nbsp;&nbsp; collect_all_answers(Answer,E,Xs).

% collects all answers of a Solver

collect_all_answers(no,_,[]).
collect_all_answers(the(X),E,[X|Xs]):-
&nbsp;&nbsp; get(E,Answer),
&nbsp;&nbsp; collect_all_answers(Answer,E,Xs).</font></font></pre>
<font size=+2>Note that, again, the <b>collect_all_answers</b> operation
is generic, and works on any <b>Source</b>. This suggest providing a built-in
Source-to-List converter <b>source_list/2</b> which can be made more efficient
in the underlying implementation language.</font>
<p>
<hr WIDTH="100%">
<h1>
Findall/3: with fluent operations</h1>

<pre><font face=""><font size=+2>findall(X,G,Xs):-&nbsp;
&nbsp;&nbsp; answer_source(X,G,Solver),
&nbsp;&nbsp; source_list(Solver,Xs).</font></font></pre>

<h1>
Term copying and instantiation state detection:</h1>
<font face="Courier New"><font size=+2>copy_term(X,CX):-first_solution(X,true,the(CX)).</font></font>
<p><font face="Courier New"><font size=+2>var(X):-copy_term(X,a),copy_term(X,b).</font></font>
<br>
<hr WIDTH="100%">
<h1>
Built-ins as a Library of Fluents</h1>

<h3>
<font size=+2>Emulating (backtrackable) dynamic database operations</font></h3>
<font face="Courier New"><font size=+2>assert_(Clause,Engines,[E|Engines]):-</font></font>
<br><font face="Courier New"><font size=+2>&nbsp;&nbsp; answer_source(repeat,Clause,E).</font></font>
<p><font face="Courier New"><font size=+2>linear_assert(Clause,Engines,[E|Engines]):-</font></font>
<br><font face="Courier New"><font size=+2>&nbsp; answer_source(true,Clause,E).</font></font>
<p><font face="Courier New"><font size=+2>clause_(Engines,Head,Body):-</font></font>
<br><font face="Courier New"><font size=+2>&nbsp;&nbsp; member(E,Engines),</font></font>
<br><font face="Courier New"><font size=+2>&nbsp;&nbsp; get(E,the((Head:-Body))).</font></font>
<h1>
Database Fluents</h1>

<h2>
Lists and Terms as Source Fluents</h2>

<pre><font face="Courier New,Courier"><font size=+2>univ(T,FXs):-if(var(T),list_to_fun(FXs,T),fun_to_list(T,FXs)).</font></font></pre>

<pre><font face="Courier New,Courier"><font size=+2>list_to_fun(FXs,T):-list_source(FXs,I),source_term(I,T).</font></font></pre>

<pre><font face="Courier New,Courier"><font size=+2>fun_to_list(T,FXs):-term_source(T,I),source_list(I,FXs).</font></font></pre>

<h2>
File, Socket,URL I/O: char and clause readers and writers.</h2>

<hr WIDTH="100%">
<h1>
Lazy Arithmetics with Fluents</h1>

<pre><font size=-1>&nbsp; </font><font face=""><font size=+2>integer_source(MaxFuel,A,X,B).</font></font></pre>
<font face=""><font size=+2>iterated computation of X&lt;-A*X+B at most
MaxFuel times</font></font>
<h1>
Memoing Fluents</h1>

<ul>
<li>
<font face=""><font size=+3>Most Fluents: usable only once, by default,
and release all resources</font></font></li>

<li>
<font face=""><font size=+3>A <b>Memoing Fluent</b> is built easily on
top of a Source Fluent by accumulating values in a List or dynamic array.</font></font></li>

<li>
<font face=""><font size=+3>A Memoing Fluent can be shared between multiple
consumers which want to avoid recomputation of a given value.</font></font></li>
</ul>

<hr WIDTH="100%">
<h1>
Fluent based Lazy Lists</h1>

<ul>
<li>
<font face=""><font size=+2>an instance of Memoing Fluents</font></font></li>

<li>
<font face=""><font size=+2>they accumulate successive values of a Source
Fluent in a (reusable) list</font></font></li>
</ul>
<font face="Courier New"><font size=+2>&nbsp;source_lazy_list(Source, LazyList</font></font><font size=-1>)</font>
<pre><font size=-1>&nbsp; </font><font face=""><font size=+2>lazy_head(LazyList, LazyHead)</font></font></pre>

<pre><font size=-1>&nbsp; </font><font face=""><font size=+2>lazy_tail(LazyList, LazyTail)</font></font>.</pre>

<h1>
Lazy <b>findall/3</b></h1>

<pre><font face=""><font size=+2>% creates lazy list form an answer source

lazy_findall(X,G,LazyList):-
&nbsp; answer_source(X,G,S),
&nbsp; source_lazy_list(S,LazyList).</font></font></pre>

<pre>
<hr WIDTH="100%"></pre>

<h1>
<b>lazy_element_of/2</b></h1>

<pre><font size=+2><font face="Courier New">% explores a lazy list in a way compatible with backtracking
% allows multiple 'consumers' to access the list, end ensures that
% the lazy list advances progressively and consistently
</font><font face="">lazy_element_of(XXs,X):-
&nbsp; lazy_decons(XXs,A,Xs),
&nbsp; lazy_select_from(Xs,A,X).

% backtracks over the lazy list
lazy_select_from(_,A,A).
lazy_select_from(XXs,_,X):-lazy_element_of(XXs,X).

% returns a head/tail pair of a non-empty lazy list
lazy_decons(XXs,X,Xs):-
&nbsp; neq(XXs,[]),
&nbsp; lazy_head(XXs,X),
&nbsp; lazy_tail(XXs,Xs).</font></font></pre>

<pre>
<hr WIDTH="100%"></pre>

<h1>
Multi Variables and Fluent based DCGs</h1>
<font size=+2>Multi-Variables are special Fluents which accumulate multiple
values on an internal stack. As the stack is popped on backtracking multi-variables
return to their previous values, therefore providing a form of backtrackable
destructive assignment. A new Multi-Variable is built with</font>
<p><b><font size=+2>def(MultiVar,InitialValue)</font></b>
<br><b><font size=+2>set(MultiVar,NewValue)</font></b>
<br><font size=+2><b>val(MultiVar,Value))</b>.</font>
<h1>
multi-stream DCGs,</h1>
&nbsp;<font face=""><font size=+3>do not require a DCG preprocessor,</font></font>
<p><font face=""><font size=+3>uses backtrackable destructive assignment
for advancing the state of a given DCG stream.</font></font>
<p><font face="Courier New"><font size=+2>dcg_def(MultiVar,Xs):-def(MultiVar,Xs).
dcg_val(MultiVar,Xs):-val(MultiVar,Xs).</font></font>
<br><font face="Courier New"><font size=+2>dcg_connect(MultiVar,X):-val(MultiVar,[X|Xs]),set(MultiVar,Xs).</font></font>
<br>
<hr WIDTH="100%">
<h1>
Towards XSB resolution: Kernel Prolog + Memoing</h1>

<ul>
<li>
<font size=+3>a global store is implemented as a Database Fluent in which
we can attach to each <i>goal variant</i> a unique persistent Answer Source</font></li>

<li>
<font size=+3>Goal variants can be represented as ground terms resulting
from the <b>numbervars/2</b> operation, on which a hash key can be computed</font></li>

<li>
<font size=+3>a <i>memoing engine</i> construct, which encapsulates the
same functionality as an ordinary <i>Answer Source</i>, from outside, while
making sure, internally, that the stream of solutions is computed only
once, and shared through copying by all the consumers in the <i>equivalence
class</i> of the answer variant</font></li>

<li>
<font size=+3>can be implemented by adapting the Lazy List construct to
persistent Fluents</font></li>
</ul>

<hr WIDTH="100%">
<h1>
<font size=+3>Adding Constraints</font></h1>

<ul>
<li>
<font size=+3>Kernel Prolog has an extensible unification algorithm (see
Multi-Variables)</font></li>

<li>
<font size=+3>Constraint Stores as Fluents: <i>put</i> + <i>collect</i></font></li>

<li>
<font size=+3>Assertional vs. Variable based&nbsp; constraints</font></li>

<li>
<font size=+3>propagation can be triggered on unification or on updating
a Linda blackboard</font></li>

<li>
<font size=+3>uniform design patterns</font></li>
</ul>

<hr WIDTH="100%">
<h1>
Related work</h1>

<ul>
<li>
<font size=+3>similar to our Answer Sources: <i>engines</i> in Oz</font></li>

<ul>
<li>
<font size=+3>main difference with Oz: we have <i>encapsulated backtracking</i></font></li>
</ul>

<li>
<font size=+3>pure subsets of Prolog: Mercury</font></li>

<li>
<font size=+3>Horn Clauses with negation have been extensively studied
- can we adapt some results to better understand Answer Sources?</font></li>
</ul>

<hr WIDTH="100%">
<h2>
<font size=+3>Future/Ongoing Work</font></h2>

<ul>
<li>
<font face=""><font size=+3>executable specification of ISO Prolog in terms
of Kernel Prolog</font></font></li>

<li>
<font face=""><font size=+3>a study of Kernel Prolog's invariance under
program transformations (unfolding)</font></font></li>

<li>
<font face=""><font size=+3>expressiveness of multi-engine Datalog (conjectured
as being Turing-equivalent)</font></font></li>

<li>
<font face=""><font size=+3>type checking / type inference mechanisms for
Kernel Prolog</font></font></li>

<li>
<font face=""><font size=+3>lightweight engine creation and engine reuse
techniques for Kernel Prolog</font></font></li>

<li>
<font face=""><font size=+3>adapting Kernel Prolog for spoken I/O, using
a new prototype based Natural Language style predicate syntax</font></font></li>

<li>
<font face=""><font size=+3>Kernel Prolog as a basis of embedded Prolog
component technology and Prolog based Palm computing</font></font></li>

<li>
<font face=""><font size=+3>implementation technique for lightweight Web
based Kernel Prolog interpreters - XML</font></font></li>
</ul>

<hr WIDTH="100%">
<h1>
<font size=+3>Conclusion</font></h1>

<ul>
<li>
<font face=""><font size=+3>uniform interoperation of Horn Clause Solvers
with stateful entities (Fluents) ranging from external procedural and object
oriented language services like I/O operations, to other, 'first class
citizen' Horn Clause Solvers</font></font></li>

<li>
<font face=""><font size=+3>as a result, a simplified Prolog built-in predicate
system has emerged</font></font></li>

<li>
<font face=""><font size=+3>Fluent Composers allow combining component
functionality in generic, data representation independent ways</font></font></li>

<li>
<font face=""><font size=+3>Lightweight Prolog implementation</font></font></li>
</ul>

<dl compact>
<hr WIDTH="100%"></dl>

<h1>
Looking through the Walls of the Glass Box: a Kernel Prolog Interpreter
in Java:</h1>

<pre><font face="Courier New,Courier"><font size=+2>&nbsp;public Term get() {
&nbsp;&nbsp;&nbsp; if(null==orStack) return null;
&nbsp;&nbsp;&nbsp; Clause answer=null;
&nbsp;&nbsp;&nbsp; while(!orStack.isEmpty()) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Unfolder I=(Unfolder)orStack.pop();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; answer=I.getAnswer();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(null!=answer) break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Clause goal=(Clause)I.get();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(null!=goal) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if( I.notLastClause() ) orStack.push(I);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else I.stop();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(null==answer)orStack.push(new Unfolder(goal,this));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; Term head;
&nbsp;&nbsp;&nbsp; if(null==answer) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; head=null;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stop();
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; else&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; head=answer.getHead();
&nbsp;&nbsp;&nbsp; return head;
&nbsp;}</font></font>





<hr WIDTH="100%"></pre>

<h1>
Unfolder class:</h1>

<pre><font face=""><font size=+1>&nbsp;</font><font size=+2>public Term get() {
&nbsp;&nbsp;&nbsp; if(null==e) return null;
&nbsp;&nbsp;&nbsp; Clause unfolded_goal=null;
&nbsp;&nbsp;&nbsp; while(e.hasMoreElements()) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Term T=(Term)e.nextElement();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; prog.getTrail().unwind(oldtop);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unfolded_goal=T.toClause().unfold_with_goal(goal,prog.getTrail());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(null!=unfolded_goal) break;
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; return unfolded_goal;
&nbsp; }</font></font></pre>

<hr WIDTH="100%">
<br>&nbsp;
<br>&nbsp;
</body>
</html>
